package com.taxsystem.yehao.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taxsystem.yehao.View.Calendar.EggCalendar;
import com.taxsystem.yehao.jsonParser.CalendarJsonParser;
import com.taxsystem.yehao.tool.UrlHost;
import com.main.taxsystem.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.Spinner;

public class CalendarActivity extends BaseListActivity {

	private EggCalendar calendarView = null;
	private ProgressBar progressBar = null;
	private Spinner spinner = null;
	private String branchId;
	private List<HashMap<String,Object>> taxTypeList = null;
	private List<HashMap<String,Object>> calendarList = null;
	private String curYear = null;
	private String curMon = null;
	private String tempYear = null;
	private String tempMonth = null;
	private String spinnerSelectedItem = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar);
		branchId = getIntent().getStringExtra("branchId");
		calendarView = (EggCalendar) findViewById(R.id.calendar);
		spinner = (Spinner) findViewById(R.id.taxtype_spinner);
		progressBar = (ProgressBar) findViewById(R.id.calendar_progressBar);
		Date curDate = calendarView.getCurDate();
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM");
		curYear = format.format(curDate).split("/")[0];
		curMon = format.format(curDate).split("/")[1];
		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				setProgressBarVisible();
				getData(curYear, branchId);
				setProgressBarInvisible();
				refreshView();
				initMon();
			}
			
		}.start();
		
		
		calendarView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					float x = event.getX();
					float y = event.getY();
					if(calendarView.isSwitchMonth(x, y)) {
						Date nowDate = calendarView.getCurDate();
						SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
						String dt = simpleDateFormat.format(nowDate);
						tempMonth = dt.split("/")[1];
						tempYear =  dt.split("/")[0];
						curMon = tempMonth;
						if(!tempYear.equals(curYear)) {
							new Thread() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									curYear = tempYear;
									setProgressBarVisible();
									getData(curYear, branchId);
									setProgressBarInvisible();
									refreshView();
								}
								
							}.start();
						}
						else {
							refreshCalendarView();
						}
					}
					break;
				case MotionEvent.ACTION_UP:
					break;
				}
				return false;
			}
		});
		spinner.setOnItemSelectedListener( new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				handler.sendEmptyMessage(2);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	public void getData(String year,String branchId) {
		Map params = new HashMap<String, Object>();
		params.put("branchId", branchId);
		params.put("year", year);
		taxTypeList = CalendarJsonParser.parseTaxType(UrlHost.host+"/app/taxcalendar/getbyyearandmonth", params);
		for(HashMap<String,Object> map:taxTypeList) {
			map.put(map.get("taxTypeName").toString(), map.get("taxTypeId").toString());
		}
		calendarList = CalendarJsonParser.parseCalendar(UrlHost.host+"/app/taxcalendar/getbyyearandmonth", params);
	}
	
	public void setSpinnerView() {
		String[] tmp = new String[20] ;
		int item_cnt = 0;
		for(HashMap<String,Object> taxType:taxTypeList) {
			tmp[item_cnt++] = taxType.get("taxTypeName").toString();
		}
		String [] items = new String[item_cnt];
		for(int i=0;i<item_cnt;++i) {
			items[i]=tmp[i];
		}
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,items);
		//System.out.println(spinner.getItemAtPosition(0).toString());
		spinner.setAdapter(aa);
		spinner.setSelection(0);
		spinnerSelectedItem = spinner.getSelectedItem().toString();
	}
	public void setCalendarView() {
		String taxType = spinnerSelectedItem;
		String taxTypeId ="";
		int st=0,en=0;
		for(HashMap<String,Object> taxTy:taxTypeList) {
			if(taxTy.get("taxTypeName").toString().equals(taxType)) {
				taxTypeId = taxTy.get(taxType).toString();
				break;
			}
		}
		int mon = Integer.parseInt(curMon);
		for(HashMap<String,Object> calendar:calendarList) {
			if(Integer.parseInt(calendar.get("month").toString())==mon
					&&calendar.get("taxTypeId").toString().equals(taxTypeId)) {
				st = Integer.parseInt(calendar.get("fromDay").toString());
				en = Integer.parseInt(calendar.get("toDay").toString());
				break;
			}
		}
		if(st==0&&en==0) {
			st=1;en=0;
		}
		calendarView.setSt(st);
		calendarView.setEn(en);
		calendarView.invalidate();
	}
	public void refreshView() {
		handler.sendEmptyMessage(1);
	}
	public void refreshCalendarView() {
		handler.sendEmptyMessage(2);
	}
	public void setProgressBarVisible() {
		handler.sendEmptyMessage(4);
	}
	public void setProgressBarInvisible() {
		handler.sendEmptyMessage(3);
	}
	public void refreshSpinnerView() {
		handler.sendEmptyMessage(5);
	}
	public void initMon() {
		String taxTypeId ="";
		String taxType = spinnerSelectedItem;
		for(HashMap<String,Object> taxTy:taxTypeList) {
			if(taxTy.get("taxTypeName").toString().equals(taxType)) {
				taxTypeId = taxTy.get(taxType).toString();
				break;
			}
		}
		for(HashMap<String,Object> calendar:calendarList) {
			if(calendar.get("taxTypeId").toString().equals(taxTypeId)) {
				int mon = Integer.parseInt(calendar.get("month").toString());
				int curmon = Integer.parseInt(curMon);
				if(mon<curmon) {
					while(mon!=curmon) {
						mon++;
						calendarView.turnToNextMonth();
					}
				}
				else {
					while(mon>curmon) {
						mon--;
						calendarView.turnToPreMonth();
					}
				}
				break;
			}
		}
	}
	private Handler handler =new Handler(){
		@Override
		public void handleMessage(Message msg){
		super.handleMessage(msg);
		switch (msg.what) {
		case 1:setSpinnerView();setCalendarView();break;
		case 2:setCalendarView();break;
		case 3:progressBar.setVisibility(View.INVISIBLE);break;
		case 4:progressBar.setVisibility(View.VISIBLE);break;
		case 5:setSpinnerView();break;
		}
		}
		};
}
